package manager;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import entity.Histogram;
import entity.Image;

public class HistogramManagerImpl implements IHistogramManager {

	/* (non-Javadoc)
	 * @see manager.IHistogramManager#histogramEqualization(entity.Histogram, entity.Image)
	 */
	@Override
	public Histogram histogramEqualization(Histogram histogram, Image image, int l) {
		 Map<Integer, Integer> newHistogram = new TreeMap<Integer, Integer>();
		 float n = 0;
		 Set<Integer> keys = histogram.getHistogram().keySet();
		 for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
			int key = (Integer) iterator.next();
			n += histogram.getHistogram().get(key);
			float value = (float)(((l-1)/(float)(image.getHeight()*image.getWidth()))*n);
			newHistogram.put(key, Math.round(value));
		}
		 return new Histogram(newHistogram);
	}

	/* (non-Javadoc)
	 * @see manager.IHistogramManager#histogramExpansion(entity.Histogram, entity.Image)
	 */
	@Override
	public Histogram histogramExpansion(Image image, int rMax, int rMin, int l) {
		int[][] matrixImage = image.getImage(); 
		Map<Integer, Integer> newHistogram = new TreeMap<Integer, Integer>();
		for (int i = 0; i < matrixImage.length; i++) {
			for (int j = 0; j < matrixImage[i].length; j++) {
				float value = ((float)(matrixImage[i][j] - rMin) / (float)(rMax - rMin))*(l-1);
				newHistogram.put(matrixImage[i][j], Math.round(value));
			}
		}
		
		return new Histogram(newHistogram);
	}

}
